// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Connection.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Forge.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// This interface embeds a very simple Connection 
    /// </summary>
    public abstract class Connection : IConnection
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Connection"/> class.
        /// </summary>
        /// <param name="layer">
        /// The layer.
        /// </param>
        public Connection(ILayer layer)
        {
            this.Layer = layer;
        }

        #endregion

        // Events handling (Connection can change of status by user willing or because of external reasons)
        #region Properties

        /// <summary>
        /// Gets Status.
        /// </summary>
        public abstract EConnectionStatus ConnectionStatus { get; }

        /// <summary>
        /// Gets or sets Layer.
        /// </summary>
        public ILayer Layer { get; set; }

        #endregion

        #region Implemented Interfaces

        #region IConnection

        /// <summary>
        /// The connect.
        /// </summary>
        public abstract Status Connect();

        /// <summary>
        /// The disconnect.
        /// </summary>
        public abstract Status Disconnect();

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// The fire connected event.
        /// </summary>
        protected void RaiseConnectedEvent()
        {
            if (this.Layer.ConnectionEventCB != null)
            {
                this.Layer.ConnectionEventCB.RaiseEvent(this.Layer, new ConnectionEventArgs(EConnectionStatus.Connected));
            }
        }

        /// <summary>
        /// The fire connecting event.
        /// </summary>
        protected void RaiseConnectingEvent()
        {
            if (this.Layer.ConnectionEventCB != null)
            {
                this.Layer.ConnectionEventCB.RaiseEvent(this.Layer, new ConnectionEventArgs(EConnectionStatus.Connecting));
            }
        }

        /// <summary>
        /// The fire disconnected event.
        /// </summary>
        protected void RaiseDisconnectedEvent()
        {
            if (this.Layer.ConnectionEventCB != null)
            {
                this.Layer.ConnectionEventCB.RaiseEvent(this.Layer, new ConnectionEventArgs(EConnectionStatus.Disconnected));
            }
        }

        /// <summary>
        /// The fire disconnecting event.
        /// </summary>
        protected void RaiseDisconnectingEvent()
        {
            if (this.Layer.ConnectionEventCB != null)
            {
                this.Layer.ConnectionEventCB.RaiseEvent(this.Layer, new ConnectionEventArgs(EConnectionStatus.Disconnecting));
            }
        }

        #endregion
    }
}